package com.zhyl.demo.controller;

import com.zhyl.demo.entity.HuaYan;
import com.zhyl.demo.entity.SendMessage;
import com.zhyl.demo.service.AppointmentService;
import com.zhyl.demo.service.DoctorService;
import com.zhyl.demo.service.HuaYanService;
import com.zhyl.demo.service.SendMessageService;
import com.zhyl.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/huayan")
public class HuaYanController {
    private final HuaYanService huaYanService;
    private final AppointmentService appointmentService;
    private final SendMessageService sendMessageService;
    private final DoctorService doctorService;

    @Autowired
    public HuaYanController(HuaYanService huaYanService,
                            AppointmentService appointmentService,
                            SendMessageService sendMessageService,
                            DoctorService doctorService) {

        this.huaYanService = huaYanService;
        this.appointmentService = appointmentService;
        this.sendMessageService = sendMessageService;
        this.doctorService = doctorService;
    }

    @RequestMapping("/addHuaYan")
    public Result addHuaYan(@RequestBody HuaYan huaYan) {
        if (huaYan==null){
            return Result.error("-1","参数错误");
        }

        Boolean b = huaYanService.addHuaYan(huaYan);
        if (b){
            //更新预约表状态
            Boolean huayan = appointmentService.huayan(huaYan.getAppointmentId());
            if (huayan){
                SendMessage sendMessage = new SendMessage();
                sendMessage.setUserId(appointmentService.getAppointmentDetail(huaYan.getAppointmentId()).getPatientId());
                sendMessage.setMessage("请凭"+huaYan.getAppointmentId()+"号到化验室进行化验");
                sendMessageService.sendMessage(sendMessage);
                return Result.success(huaYan.getId());
            }else {
                return Result.error("-1","添加失败");
            }
        }else {
            return Result.error("-1","添加失败");
        }
    }

    @RequestMapping("/getHuaYan")
    public Result getHuaYan(Integer appointmentId) {
        if (appointmentId==null){
            return Result.error("-1","参数错误");
        }
        ArrayList<HuaYan> huaYan = huaYanService.getHuaYan(appointmentId);
        if (huaYan==null){
            return Result.error("-1","未查询到数据");
        }
        return Result.success(huaYan);
    }

    @RequestMapping("/updateHuaYan")
    public Result updateHuaYan(@RequestBody HuaYan huaYan) {

        if (huaYan==null){
            return Result.error("-1","参数错误");
        }
        Boolean b = huaYanService.updateHuaYan(huaYan);
        if (b){
            Boolean b1 = appointmentService.cancelCall(huaYan.getAppointmentId());
            if (b1){
                SendMessage sendMessage = new SendMessage();
                sendMessage.setUserId(appointmentService.getAppointmentDetail(huaYan.getAppointmentId()).getPatientId());
                sendMessage.setMessage("化验结果已出，等待医生重新叫号");
                sendMessageService.sendMessage(sendMessage);
                sendMessage.setUserId(doctorService.docInfo(appointmentService.getAppointmentDetail(huaYan.getAppointmentId()).getDoctorId()).getUserDoc());
                sendMessage.setMessage("化验结果已出，请对病人继续诊断");
                sendMessageService.sendMessage(sendMessage);
            return Result.success(huaYan.getId());
            }else {
                return Result.error("-1","修改失败");
            }
        }else {
            return Result.error("-1","修改失败");
        }
    }

    @RequestMapping("/getAllHuaYan")
    public Result getAllHuaYan() {

        ArrayList<HuaYan> huaYan = huaYanService.getAllHuaYan();
        if (huaYan==null){
            return Result.error("-1","未查询到数据");
        }
        return Result.success(huaYan);
    }
}
